<?php
//自己的异常处理器
class ExceptionFrameLib extends Exception {
	
	public function __construct($message,$code=0,$extra=false) {
		parent::__construct($message,$code);
		$this->type = get_class($this);
		$this->extra = $extra;
	}
	
	
	static public function appError($errno, $errstr, $errfile, $errline) {
		if(ACCESS_TYPE == 'SHELL')
			$s = "\n";
		else
			$s = '<br/>';
		
		$trace = debug_backtrace();
		$e = "error info:".$errstr .$s;
		$e .= "file:".$errfile .$s;
		$e .= "line:".$errline .$s;
		
		$traceInfo = self::errInfo($trace);
		
		self::halt($e.$traceInfo,'SYSTEM');
	}

	static public function errInfo($trace){
		if(ACCESS_TYPE == 'SHELL')
			$s = "\n";
		else
			$s = '<br/>';
		
		//	var_dump($trace);
		$e = "";
		if(isset($trace[0]['file']))
			$e .= "file:".$trace[0]['file'] .$s;
		
		if(isset($trace[0]['function']))
			$e .= "function:".$trace[0]['function'] .$s;;
			
		if(isset($trace[0]['line']))
			$e .= 'line:'.$trace[0]['line'] ." ".$s;
			
			
		$traceInfo = '';
		$time = date('y-m-d H:i:m');
		foreach ($trace as $t) {
			$traceInfo .= '[' . $time . '] ';
		
			if(isset($t['line']))
				$traceInfo .=  ' (' . $t['line'] . ') ';
		
			if(isset($t['file']))
				$traceInfo .= $t['file'] . " ";
		
			if(isset($t['class']))
				$traceInfo .= $t['class'] . " ";
		
			if(isset($t['type']))
				$traceInfo .= $t['type'] . " ";
		
			if(isset($t['function']))
				$traceInfo .= $t['function'] . " ";
				
			// 					$traceInfo .= implode(',', $t['args']);
			$traceInfo .=')'.$s;
		}
		
		return $traceInfo;
	}
	
	static public function appException($e) {
		var_dump(232323);
		//$this->halt($e->__toString());
	}

	static function halt($error , $module = '' ,$back_url = '') {
		if('SHELL' == ACCESS_TYPE){
			$trace = debug_backtrace();
			$traceInfo = self::errInfo($trace);
			$error =$error ."\n". $traceInfo;
			var_dump($error);
			exit;
		}
		if(strpos($error,"action_log")){
			echo "loop....";var_dump($error);	
			exit;
		}
		
		LogLib::errorWrite($error,$module);
		
		$title = "错误提示";
		$e = $error;
		
		$tplt = getSmartyObject();
		$tplt->checkErrorFile();
		
		include $tplt->compile('error.html');
		exit;
	}
}
